package com.d3.pomodoro.component

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.d3.pomodoro.R
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.view_tomato_clock.view.*
import me.shaohui.bottomdialog.BottomDialog

class TomatoClockView : ConstraintLayout, View.OnClickListener {
    private var timeCounter: TimeCounter? = null

    private var tomatoClockViewListener: TomatoClockViewListener? = null

    companion object {
        const val JUST_TOMATO_TIME = 25 * 60 * 1000L
        const val RELEASE_TOMATO_TIME = 5 * 60 * 1000L
        const val INTERVAL_TOMATO_TIME = 1000L

        const val ACTION_ZOOM_IN = "ACTION_ZOOM_IN"
        const val ACTION_ZOOM_OUT = "ACTION_ZOOM_OUT"
        const val ACTION_CLICK_MENU = "ACTION_CLICK_MENU"
        const val ACTION_START_CLOCK = "ACTION_START_CLOCK"
        const val ACTION_CLICK_STOP = "ACTION_CLICK_STOP"
        const val ACTION_TIMER_FINISH = "ACTION_TIMER_FINISH"

    }

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )


    init {
        inflate(context, R.layout.view_tomato_clock, this)
        zoomImageView.setOnClickListener(this)
        menuImageView.setOnClickListener(this)
        timeClockButton.setOnClickListener(this)
        sbStop.setOnClickListener(this)
        titleTextView.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when {
            v?.id == R.id.zoomImageView -> {
                itemRootView.visibility = View.GONE
                timeClockButton.visibility = View.VISIBLE
                tomatoClockViewListener?.action(ACTION_ZOOM_IN)

            }

            v?.id == R.id.timeClockButton -> {
                itemRootView.visibility = View.VISIBLE
                timeClockButton.visibility = View.GONE
                tomatoClockViewListener?.action(ACTION_ZOOM_OUT)

            }

            v?.id == R.id.menuImageView -> {
                tomatoClockViewListener?.action(ACTION_CLICK_MENU)

                val tomatoClockTipView = TomatoClockTipView()
                tomatoClockTipView.setTomatoClockTipViewListener(object :
                    TomatoClockTipView.TomatoClockTipViewListener {
                    override fun action(action: String) {
                        tomatoClockTipView.dismiss()
                    }
                })
                if (context != null && context is AppCompatActivity) {
                    tomatoClockTipView.show((context as AppCompatActivity).supportFragmentManager)
                }

            }

            v?.id == R.id.sbStop -> {
                tomatoClockViewListener?.action(ACTION_CLICK_STOP)
            }

            v?.id == R.id.titleTextView -> {

            }
        }
    }

    fun startTomatoClock(time: Long) {
        visibility = View.VISIBLE
        progressBar.max = time.toInt()
        timeCounter = TimeCounter(time, INTERVAL_TOMATO_TIME, timeClockButton,
            clockTextView, object : TimeCounter.TimeCounterListener {
                override fun onTick(current: Long, total: Long) {
                    progressBar.progress = (total - current).toInt()
                }

                override fun onFinish() {
                    tomatoClockViewListener?.action(ACTION_TIMER_FINISH)
                }
            })
        timeCounter?.start()
        tomatoClockViewListener?.action(ACTION_START_CLOCK)
    }

    fun stopTomatoClock() {
        releaseTimeCounter()
        timeCounter = null
        visibility = View.GONE
    }

    fun releaseTimeCounter() {
        timeCounter?.cancel()
    }

    fun setTomatoClockViewListener(tomatoClockViewListener: TomatoClockViewListener?) {
        this.tomatoClockViewListener = tomatoClockViewListener
    }

    interface TomatoClockViewListener {
        fun action(action: String)
    }


}